// pages/mockexam/theory/post/detail/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    post:'',
    subjectList:[],
    subjectIndex:0,
    questionIndex:0,
    showButton:false,
    flag:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      post: options
    })
    wx.setNavigationBarTitle({
      title: options.title,
    })
    var _this=this;
    app.paiRequest("/app/mockexam/theory/subjectList", {
      parentCode: options.id
    }).then(function (ress) {
      var subjectList = ress.data;
      _this.setData({
        subjectList: subjectList
      })      
    })
    app.paiRequest("/app/mockexam/theory/user/save", {
      postId: options.id,
      theoryId: options.theoryId
    }).then(function (res) {
      _this.setData({
        theoryUserId: res.data.id
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  upList:function(){
    var questionIndex = this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList = subjectList[subjectIndex].quetbankList;
    var questionSize = quetbankList.length;
    var subjectSize = subjectList.length;
    if (questionIndex>0){
      this.setData({
        questionIndex: questionIndex - 1
      })
    } else if (questionIndex==0){
      subjectIndex = subjectIndex-1;      
      if (subjectIndex>0){
        var listqt = subjectList[subjectIndex].quetbankList;
        this.setData({
          subjectIndex: subjectIndex,
          questionIndex: listqt.length-1
        })
      } else if (subjectIndex==0){
        var listqu = subjectList[subjectIndex].quetbankList;
        this.setData({
          subjectIndex: subjectIndex,
          questionIndex: listqu.length-1
        })
      }else{
        this.setData({
          subjectIndex: 0,
          questionIndex: 0
        })
      }
    }else{
      this.setData({
        questionIndex: 0
      })
    }
  },
  nextList:function(){
    var questionIndex=this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList=subjectList[subjectIndex].quetbankList;
    var questionSize = quetbankList.length;
    var subjectSize = subjectList.length;
    if (questionIndex >=0) {
      questionIndex=questionIndex + 1;
      if (questionIndex>=questionSize){
        subjectIndex = subjectIndex+1;
        if (subjectIndex >= subjectSize){
          this.setData({
            showButton:true
          })
        }else{
          this.setData({
            subjectIndex: subjectIndex,
            questionIndex:0
          })
        }
      }else{
        this.setData({
          questionIndex: questionIndex
        })
      }
      
    }else{
      this.setData({
        questionIndex: 0
      })
    }
  },
  radioChang:function(e){
    console.log(e);
    var value=e.detail.value;
    var questionIndex = this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList = subjectList[subjectIndex].quetbankList; 
    var question = quetbankList[questionIndex];
    question.answer=value;
    quetbankList[questionIndex] = question;
    subjectList[subjectIndex].quetbankList = quetbankList;
    this.setData({
      subjectList: subjectList
    })
    var options=this.data.post;
    var questionId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    app.paiRequest("/app/mockexam/theory/record/saveRecord", {
      theoryId: options.theoryId,
      questionId: questionId,
      answerId: value,
      questionType: type,
      postId: options.id
    }).then(function (res) {
      console.log(res)
    })
  },
  checkboxChang:function(e){
    console.log(e);
    var value = e.detail.value;
    var questionIndex = this.data.questionIndex;
    var subjectIndex = this.data.subjectIndex;
    var subjectList = this.data.subjectList;
    var quetbankList = subjectList[subjectIndex].quetbankList;
    var question = quetbankList[questionIndex];
    question.answer1 = 0;
    question.answer2 = 0;
    question.answer3 = 0;
    question.answer4 = 0;
    question.answer5 = 0;
    for (var i = 0; i < value.length;i++){
      var a=value[i];
      question['answer'+a] = a;
    }
    quetbankList[questionIndex] = question;
    subjectList[subjectIndex].quetbankList = quetbankList;
    this.setData({
      subjectList: subjectList
    })

    var options = this.data.post;
    var questionId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    app.paiRequest("/app/mockexam/theory/record/saveRecord", {
      theoryId: options.theoryId,
      questionId: questionId,
      answerId: value,
      questionType: type,
      postId: options.id
    }).then(function (res) {
      console.log(res)
    })
  },
  submitQuestion: function () {
    var _this = this;
    var options = this.data.post;
    var theoryUserId = this.data.theoryUserId;
    app.paiRequest("/app/mockexam/theory/compute", {
      theoryUserId: theoryUserId,
      theoryId: options.theoryId,
      postId:options.id
    }).then(function (res) {
      console.log(res)
      var data = res.data;
      _this.setData({
        resultInfo: data,
        flag: false
      })
    })
  },
  conceal: function () {
    this.setData({
      flag: true
    })
  },
  deleteRecord:function(e){
    var src = "/pages/mine/examination/mocktheorypost/detail/index";
    var post=this.data.post;
    wx.navigateTo({
      url: src+'?id=' + post.id + '&title=' + post.title + '&theoryId=' + post.theoryId,
    })
  }
})